﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PointTracker
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        public List<Student> students;
        public List<TextBox> textBoxes;
        public List<CheckBox> checkBoxes;
        public int classCRN;
        public UserRole role;

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                if (Session["User"] == null)
                {
                    Response.Redirect("/");
                    //role = UserRole.Guest;
                }
                else if (((User)Session["User"]).role != UserRole.Professor && ((User)Session["User"]).role != UserRole.Scribe)
                {
                    Response.Redirect("/");
                    //role = ((User)Session["User"]).role;
                }
                role = ((User)Session["User"]).role;
            }

            if (Request.QueryString.Count > 0)
                classCRN = Int32.Parse(Request.QueryString["class"]);
            else
                Response.Redirect("/Error/Default.aspx");

            // Get scribe names for this class, only if the user is a professor
            if (role == UserRole.Professor)
            {
                List<List<String>> scribesForThisClass = PointTracker.DatabaseManager.getUsers(classCRN, (int)UserRole.Scribe);
                string scribeNames = "";
                foreach (List<String> scribe in scribesForThisClass)
                {
                    if (!scribeNames.Equals(""))
                        scribeNames += ", ";
                    scribeNames += scribe[0] + " " + scribe[1];
                }
                if (!scribeNames.Equals(""))
                    scribeNames += "<br />";
                divScribeNames.InnerHtml = scribeNames + "<a style='display:block;' href='/addScribe.aspx?class=" + classCRN.ToString() + "'>Add a scribe to this class</a><br />";
            }

            // Get student information for this class
            students = PointTracker.Student.getStudents(classCRN);
            if (students.Count() > 0)
            {
                textBoxes = new List<TextBox>();
                checkBoxes = new List<CheckBox>();

                Table table = new Table();
                table.ID = "studentTable";
                Page.Form.Controls.Add(table);

                TableRow headerRow = new TableRow();

                TableHeaderCell hCellLname = new TableHeaderCell();
                hCellLname.Text = "Last Name";
                hCellLname.Width = 100;
                hCellLname.Style.Add("font-weight", "bold");
                TableHeaderCell hCellFname = new TableHeaderCell();
                hCellFname.Text = "First Name";
                hCellFname.Width = 100;
                hCellFname.Style.Add("font-weight", "bold");
                TableHeaderCell hCellPoints = new TableHeaderCell();
                hCellPoints.Text = "Points";
                hCellPoints.Width = 50;
                hCellPoints.Style.Add("font-weight", "bold");

                headerRow.Cells.Add(hCellLname);
                headerRow.Cells.Add(hCellFname);
                headerRow.Cells.Add(hCellPoints);

                if (role == UserRole.Professor)
                {
                    TableHeaderCell hCellDelete = new TableHeaderCell();
                    hCellDelete.Text = "Delete Student?";
                    hCellDelete.Width = 100;
                    hCellDelete.Style.Add("font-weight", "bold");
                    headerRow.Cells.Add(hCellDelete);
                }
                table.Controls.Add(headerRow);

                foreach (Student student in students)
                {
                    TableRow tempRow = new TableRow();

                    TextBox tBox = new TextBox();
                    tBox.Text = student.points.ToString();
                    tBox.Attributes.Add("studentId", student.duId);
                    tBox.Columns = 3;
                    tBox.Rows = 1;
                    tBox.MaxLength = 8;
                    if(role == UserRole.Guest)
                        tBox.Enabled = false;
                    textBoxes.Add(tBox);

                    TableCell tempCellLname = new TableCell();
                    tempCellLname.Text = student.lname;
                    TableCell tempCellFname = new TableCell();
                    tempCellFname.Text = student.fname;
                    TableCell tempCellPoints = new TableCell();
                    tempCellPoints.Controls.Add(tBox);

                    tempRow.Cells.Add(tempCellLname);
                    tempRow.Cells.Add(tempCellFname);
                    tempRow.Cells.Add(tempCellPoints);

                    if (role == UserRole.Professor)
                    {
                        CheckBox cBox = new CheckBox();
                        cBox.Enabled = true;
                        checkBoxes.Add(cBox);
                        TableCell tempCellCbox = new TableCell();
                        tempCellCbox.Controls.Add(cBox);
                        tempRow.Cells.Add(tempCellCbox);
                    }

                    table.Controls.Add(tempRow);
                }

                divStudents.Controls.Add(table);
            }
            else
            {
                if (role == UserRole.Professor)
                {
                    ButtonUpdate.Visible = false;
                    divStudents.InnerHtml = "There are no students assigned to this class<br />Click <a href='/addRemoveStudent.aspx?class=" + classCRN.ToString() + "'>here</a> to add students to this class";
                }
            }
        }

        public void UpdatePoints(Object sender, EventArgs e)
        {
            for (int x = 0; x < students.Count(); x++)
            {
                Int32 Num = 0;
                bool isNum = false;
                if (role == UserRole.Professor && !checkBoxes[x].Checked)
                {
                    checked
                    {
                        isNum = Int32.TryParse(textBoxes[x].Text, out Num);
                        if (isNum)
                            students[x].points = Num;//Int32.Parse(textBoxes[x].Text);
                    }
                }
                else if (role == UserRole.Scribe)
                    checked
                    {
                        isNum = Int32.TryParse(textBoxes[x].Text, out Num);
                        if (isNum)
                            students[x].points = Int32.Parse(textBoxes[x].Text);
                    }
            }

            Boolean successful = PointTracker.DatabaseManager.updatePoints(students, classCRN);

            if (role == UserRole.Professor)
            {
                for (int x = 0; x < checkBoxes.Count(); x++)
                {
                    if (checkBoxes[x].Checked)
                    {
                        PointTracker.DatabaseManager.deleteStudent(students[x].fname, students[x].lname, students[x].duId);
                    }
                }
            }

            if (successful)
            {
                if (role == UserRole.Professor)
                    Response.Redirect("/Professor.aspx");
                else if (role == UserRole.Scribe)
                    Response.Redirect("/Scribe.aspx");
                //else
                  //  Response.Write("neither");
            }
            else
            {
                Response.Redirect("~/Error/Default.aspx");
            }
        }
    }
}